/*
 * @Description:
 * @Author: dh
 * @Date: 2022-07-18 14:02:30
 * @LastEditors: dh
 * @LastEditTime: 2022-07-18 14:07:18
 */
import { ref } from 'vue';
import axios from 'axios';

function useURLLoader<T>(url: string) {
	const result = ref<T | null>(null);
	const loading = ref(true);
	const loaded = ref(false);
	const error = ref(null);

	axios
		.get(url)
		.then(res => {
			loading.value = false;
			loaded.value = true;
			result.value = res.data;
		})
		.catch(e => {
			error.value = e;
			loading.value = false;
		});

	return {
		result,
		loading,
		error,
		loaded,
	};
}

export default useURLLoader;
